package com.qpf

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

builder.langs(count: '3') {
    lang(flavor: 'static', version: '1.8', 'Java')
    lang(flavor: 'dynamic', version: '1.6.0', 'Groovy')
    lang(flavor: 'dynamic', version: '1.9', 'Javascript')
}

def xml =  writer.toString()
println(xml)
println('============================================')
def parser = new XmlParser()

def doc = parser.parseText(xml)
println(doc)

doc.each {
    lang ->
        println("${lang} ==> ${lang.value()}")
}

def langs = new XmlSlurper().parseText(xml)

// 平行遍历
println("langs name is ${langs.name()}, count: ${langs.@count}")
langs.lang.each {
    lang -> println("lang: ${lang.text()}, version: ${lang.@version}")
}
// 深度遍历
langs.depthFirst().findAll {
    lang -> lang.@flavor == 'dynamic'
}.collect {
    node -> println("dynamic: ${node.text()}: ${node.@version}")
}
// 广度遍历
langs.children().findAll {
    node -> node.@flavor == 'static'
}.collect {
    node -> println("static: ${node.text()}: ${node.@version}")
}